package com.hxzy.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.common.consts.RedisConst;
import com.hxzy.dto.system.SysDictTypeDTO;
import com.hxzy.entity.system.SysDictType;
import com.hxzy.mapper.system.SysDictTypeMapper;
import com.hxzy.redis.cache.DictDataCache;
import com.hxzy.service.system.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典
 */
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService {


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DictDataCache dictDataCache;


    @Override
    public List<SysDictType> selectDictTypeList(SysDictTypeDTO sysDictTypeDTO) {
         return this.baseMapper.search(sysDictTypeDTO);
    }

    /**
     * 更新状态
     * @param dictIds
     * @return
     */
    @Override
    public boolean deleteDictTypeByIds(Long[] dictIds) {
        int count=0;
        for (Long dictId : dictIds){
            //更新状态
            int result=super.baseMapper.updateByState(dictId,"1");
            count+=result;
        }


        //先查询id查询到值
        LambdaQueryWrapper<SysDictType> query=new LambdaQueryWrapper<>();
        query.in(SysDictType::getDictId,dictIds);
        query.select(SysDictType::getDictId,SysDictType::getDictType);
        List<SysDictType> sysDictTypeList = this.baseMapper.selectList(query);

        //删除redis中的数据,同步数据
        List<String> removeRedisKey=new ArrayList<>();
        for(SysDictType dictType: sysDictTypeList) {
            String redisKey= RedisConst.getRedisKey(RedisConst.DICT_DATA_KEY,dictType.getDictType());
            removeRedisKey.add(redisKey);
        }
        this.redisTemplate.delete(removeRedisKey);

        return count>0;
    }

    /**
     * 修改
     * @param sysDictType  本次要修改的
     * @param dbDict 数据库的值
     * @return
     */
    @Override
    public boolean updateDict(SysDictType sysDictType, SysDictType dbDict) {
        //更新数据库
         boolean result= this.updateById(sysDictType);
         if(result){
             // 同步redis的缓存
             //判断当前状态state=?  如果==1  二话不说，把redis中的key删除
             if(sysDictType.getStatus().equals("1")){
                 this.dictDataCache.deleteByDictType(sysDictType.getDictType());
             }
         }
        return  result;
    }

    @Override
    public boolean addDict(SysDictType sysDictType) {
        boolean result=this.save(sysDictType);
        //同步数据，
        //        //先查询id查询到值
        LambdaQueryWrapper<SysDictType> query=new LambdaQueryWrapper<>();
        query.in(SysDictType::getDictId,sysDictType.getDictId());
        query.select(SysDictType::getDictId,SysDictType::getDictType);
        List<SysDictType> sysDictTypeList = this.baseMapper.selectList(query);


        //删除redis中的数据,同步数据
        List<String> removeRedisKey=new ArrayList<>();
        for(SysDictType dictType: sysDictTypeList) {
            String redisKey= RedisConst.getRedisKey(RedisConst.DICT_DATA_KEY,dictType.getDictType());
            removeRedisKey.add(redisKey);
        }
        this.redisTemplate.delete(removeRedisKey);

        return result;
    }


}
